package com.opmsys.service.impl;
import com.opmsys.mapper.MaintainMapper;
import com.opmsys.pojo.dto.PagingSearchAboutMaintain;
import com.opmsys.pojo.entity.Maintain;
import com.opmsys.service.MaintainService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaintainServiceImpl implements MaintainService {

    final private MaintainMapper maintainMapper;

    public MaintainServiceImpl(MaintainMapper mainMapper) {
        this.maintainMapper = mainMapper;
    }


    /**
     * 新增保养记录
     * @param maintain
     * @return
     */
    @Override
    public boolean addMaintain(Maintain maintain) {
        String estimatedDate=maintain.getEstimatedDate();
        String realDate=maintain.getRealDate();
        String maintainItems=maintain.getMaintainItems();
        String maintainDetails = maintain.getMaintainDetails();
        Long personNumber = maintain.getPersonNumber();
        Integer pumpId = maintain.getPumpId();

        return maintainMapper.addMaintain(estimatedDate,realDate,maintainItems,maintainDetails,personNumber,pumpId)==1;
    }

    /**
     * 更新保养记录
     * @param maintain
     * @return
     */
    @Override
    public boolean upateMaintain(Maintain maintain) {
        Integer id = maintain.getId();
        String estimatedDate=maintain.getEstimatedDate();
        String realDate=maintain.getRealDate();
        String maintainItems=maintain.getMaintainItems();
        String maintainDetails = maintain.getMaintainDetails();
        Long personNumber = maintain.getPersonNumber();
        Integer pumpId = maintain.getPumpId();
        return maintainMapper.update(id,estimatedDate,realDate,maintainItems,maintainDetails,personNumber,pumpId)==1;
    }

    /**
     * 分页查询保养记录
     * @param pagingSearchAboutMaintain
     * @return
     */
    @Override
    public List<Maintain> selectMaintain(PagingSearchAboutMaintain pagingSearchAboutMaintain) {

        Integer id = pagingSearchAboutMaintain.getId();
        String estimatedDate=pagingSearchAboutMaintain.getEstimatedDate();
        String realDate=pagingSearchAboutMaintain.getRealDate();
        String maintainItems=pagingSearchAboutMaintain.getMaintainItems();
        String maintainDetails = pagingSearchAboutMaintain.getMaintainDetails();
        Long personNumber = pagingSearchAboutMaintain.getPersonNumber();
        Integer pumpId = pagingSearchAboutMaintain.getPumpId();
        Integer pageSize = pagingSearchAboutMaintain.getPageSize();
        Integer pageNum = (pagingSearchAboutMaintain.getPage()-1)*pageSize;
        return maintainMapper.select(id,estimatedDate,realDate,maintainItems,maintainDetails,personNumber,pumpId,pageNum,pageSize);
    }

    /**
     * 批量删除保养记录
     * @param idList
     * @return
     */
    @Override
    public int deleteMaintain(List<Integer> idList) {
        int count = 0;
        for(Integer id:idList){
            count+=maintainMapper.delete(id);
        }
        return count;
    }

    /**
     * 获取分页前按条件查询的全部记录数
     * @param pagingSearchAboutMaintain
     * @return
     */
    @Override
    public int pageTotal(PagingSearchAboutMaintain pagingSearchAboutMaintain) {
        Integer id = pagingSearchAboutMaintain.getId();
        String estimatedDate=pagingSearchAboutMaintain.getEstimatedDate();
        String realDate=pagingSearchAboutMaintain.getRealDate();
        String maintainItems=pagingSearchAboutMaintain.getMaintainItems();
        String maintainDetails = pagingSearchAboutMaintain.getMaintainDetails();
        Long personNumber = pagingSearchAboutMaintain.getPersonNumber();
        Integer pumpId = pagingSearchAboutMaintain.getPumpId();
        Integer pageSize = pagingSearchAboutMaintain.getPageSize();
        Integer pageNum = (pagingSearchAboutMaintain.getPage()-1)*pageSize;
        List<Maintain> maintains = maintainMapper.selectAll(id, estimatedDate, realDate, maintainItems, maintainDetails, personNumber, pumpId, pageNum, pageSize);
        return maintains.size();
    }


}
